import requests
import json
import random
import string
import time

f = open('char.txt', "r", encoding='utf-8')
common_chinese_chars = f.readline()


def generate_random_string(length):
    letters = string.ascii_letters

    return ''.join(random.choice(letters) for _ in range(length))


def generate_random_chinese(length):
    return ''.join(random.choices(common_chinese_chars, k=length))

def generate_options(option_count):
    options = {}
    letters = string.ascii_uppercase[:option_count]

    for letter in letters:
        option = ''.join(random.choices(common_chinese_chars, k=random.randint(3,6)))
        options[letter] = option

    return json.dumps(options, ensure_ascii=False)

def generate_options_ans(options_cnt, ans_cnt):
    letters = string.ascii_uppercase[:options_cnt]
    ans = sorted(random.sample(letters, ans_cnt))
    return json.dumps(ans)

def generate_random_blank():
    blank_cnt = random.randint(1, 5)
    ans = json.dumps([generate_random_chinese(random.randint(3, 5)) for _ in range(blank_cnt)], ensure_ascii=False)
    authority = random.randint(0, 1)
    stem = generate_random_chinese(random.randint(10, 20))
    name = generate_random_chinese(5)
    label = ''.join(random.choices(common_chinese_chars, k=2))
    analysis = generate_random_chinese(20)

    data = {
            "blank": {
                "blank_cnt": blank_cnt,
                "ans": ans
                },
            "type": "FBQ",
            "authority": authority,
            "stem": stem,
            "name": name,
            "label": label
            }

    return json.dumps(data)


def generate_random_choice():
    options_cnt = random.randint(2, 6)
    options = generate_options(options_cnt)
    ans_cnt = random.randint(1, options_cnt)
    ans = generate_options_ans(options_cnt, ans_cnt)
    authority = random.randint(0, 1)
    stem = generate_random_chinese(random.randint(10, 20))
    name = generate_random_chinese(5)
    label = ''.join(random.choices(common_chinese_chars, k=2))
    analysis = generate_random_chinese(20)

    data = {
            "choice": {
                "type": 2,
                "options_cnt": options_cnt,
                "options": options,
                "ans_cnt": ans_cnt,
                "ans": ans
                },
            "type": "CQ",
            "authority": authority,
            "stem": stem,
            "name": name,
            "label": label,
            }

    return json.dumps(data, ensure_ascii=False)

def gen_user_info():
    username = generate_random_string(5)
    password = generate_random_string(5)
    email = generate_random_string(5) + '@' + generate_random_string(5) + '.com'
    data = {
            "username": username,
            "password": password,
            "email" :email
            }
    return json.dumps(data)


def crawl_website():
    em_url = 'http://127.0.0.1:8000/user/em/'
    try:
        if True:
            # data = generate_random_blank()
            data = generate_random_choice()
            em_response = requests.post(em_url, json=json.loads(data), timeout=3)
            if em_response.status_code == 201:
                print(em_response.json())
            else:
                print('请求失败，状态码：', em_response.status_code)
        else:
            print('登录失败，状态码：', login_response.status_code)
    except requests.exceptions.Timeout:
        print('请求超时')
    except requests.exceptions.RequestException as e:
        print('请求发生异常：', e)

def gen_user():
    url = 'http://127.0.0.1:8000/user/register/'
    try:
        if True:
            # data = generate_random_blank()
            data = gen_user_info()
            em_response = requests.post(url, json=json.loads(data), timeout=3)
            if em_response.status_code == 201:
                print(em_response.json())
            else:
                print('请求失败，状态码：', em_response.status_code)
        else:
            print('登录失败，状态码：', login_response.status_code)
    except requests.exceptions.Timeout:
        print('请求超时')
    except requests.exceptions.RequestException as e:
        print('请求发生异常：', e)


if __name__ == '__main__':
    # print(generate_random_choice())
    for i in range(100):
        time.sleep(0.02)
        # crawl_website()
        gen_user()
